<template>
  <div>
    <el-card style="margin-bottom: 10px">
      <el-card>
        <div v-if="!flag">
          <span>选择考评：</span>
          <el-select v-model="templateValue" placeholder="选择考评" style="margin-right: 5px">
            <el-option
                v-for="item in templateOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div v-if="flag"><span>没有正在进行的考评</span></div>
      </el-card>

      <el-card style="margin-top: 5px">
        <div v-if="!schoolFlag">
          <span>选择学校：</span>
          <el-select v-model="schoolValue" placeholder="选择考评对象" style="margin-right: 5px">
            <el-option
                v-for="item in schoolOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div v-if="schoolFlag"><span>当前考评任务已经全部完成</span></div>
      </el-card>
    </el-card>

    <el-card>
      <el-collapse v-for="item in tableData">
        <el-collapse-item :title="item.content">
          <el-collapse v-for="items in item.child">
            <el-collapse-item :title="items.content" style="margin-left: 30px">
              <el-collapse v-for="content in items.child" style="margin-left: 30px">
                <el-collapse-item :title="content.content">
                  <el-form>
                    <div>
                      <span>自评：{{ content.score }}</span><br>
                      <spa>佐证材料（点击标签查看）:</spa><br>
                      <el-tag
                          :key="tag.id"
                          v-for="tag in content.files"
                          :disable-transitions="false"
                          style="margin-right: 5px; margin-top: 5px;cursor: pointer"
                          @click="fileView(tag.path)">
                        <a style="text-decoration: none; color: #001529; cursor: pointer">{{ tag.name }}</a>
                      </el-tag>
                    </div>
                    <el-form-item inline>
                      <div style="display: flex; justify-content: flex-end; margin-right: 30px">
                        <el-input style="width: 90px;margin-right: 10px;" placeholder="评估"
                                  v-model="records.score"></el-input>
                        <div>
                          <el-button type="primary" @click="updateRecord(content)">保存</el-button>
                        </div>
                      </div>
                    </el-form-item>
                  </el-form>
                </el-collapse-item>
              </el-collapse>
            </el-collapse-item>
          </el-collapse>
        </el-collapse-item>
      </el-collapse>
      <div style="display: flex; justify-content: flex-end" v-if="tableData.length !== 0">
        <el-button type="primary" @click="updateSuccess">考评完成</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/urils/request";
import {Base64} from "js-base64";

export default {
  name: "Assess",
  data() {
    return {
      tableData: [],
      templateOptions: [],
      templateValue: null,
      schoolOptions: [],
      schoolValue: null,

      flag: false,
      schoolFlag: false,
      fileList: [],
      records: {
        criterionId: '',
        schoolId: '',
        score: ''
      }
    }
  },
  methods: {
    fileView(path) {
      var fileName = path.split("/").pop()
      console.log(fileName)
      // var previewUrl = 'http://124.222.214.139:9090/file/download/6c68b1299b714e99a22a3fbf336a28aa.jpg' + '&fullfilename=' + '6c68b1299b714e99a22a3fbf336a28aa.jpg'
      // var previewUrl = 'http://124.222.214.139:9090/file/download/6c68b1299b714e99a22a3fbf336a28aa.jpg' + '&fullfilename=' + '6c68b1299b714e99a22a3fbf336a28aa.jpg'
      var previewUrl = path + '&fullfilename=' + fileName
      window.open("http://124.222.214.139:8012/onlinePreview?url=" + encodeURIComponent(Base64.encode(previewUrl)));
      // window.open('http://124.222.214.139:8012/onlinePreview?url=' + encodeURIComponent(Base64.encode(path)));
    },
    loadTemplate() {
      request.get('/evaluation/getActive').then(res => {
        if (res.code === '200') {
          if (res.data.length === 0) {
            this.flag = true
          } else {
            this.flag = false
            for (let i in res.data) {
              this.templateOptions.push({
                value: res.data[i].templateId,
                label: res.data[i].name
              })
            }
          }
        }else{
          this.$message.error(res.msg)
        }
      })
    },
    loadSchool() {
      let schoolId = JSON.parse(localStorage.getItem("user")).id
      request.get("/schoolGroup/selectSchool/" + schoolId).then(res => {
        if (res.code === '200') {
          if (res.data.length === 0) {
            this.schoolFlag = true
          } else {
            for (let i in res.data) {
              this.schoolOptions.push({
                value: res.data[i].userId,
                label: res.data[i].userName
              })
            }
          }
        }
      })
    },
    getData(value) {
      if (this.schoolValue === null) {
        this.$message.error("请选择考评对象")
        location.reload()
        return false
      }
      console.log(this.templateValue)
      console.log(value)
      request.get('/criterion/selectScoreTree', {
        params: {
          id: this.templateValue,
          userId: value
        }
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    updateRecord(content){
      this.records.criterionId = content.id
      this.records.schoolId = this.schoolValue;
      request.post('/evaluation/updateExpertRecord', this.records).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.records.score = null
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    updateSuccess(){
      request.get("/evaluation/schoolFinish", {
        params: {
          schoolId: this.schoolValue,
          expertId: JSON.parse(localStorage.getItem("user")).id
        }
      }).then(res => {
        if(res.code === '200'){
          location.reload();
          this.$message.success("提交成功")
        }else{
          this.$message.error(res.msg)
        }
      })
    }
  },
  created() {
    console.log(this.tableData.length)
    this.loadTemplate()
    this.loadSchool()
  },
  watch: {
    schoolValue: function (newVal, oldVal) {
      this.getData(newVal)
    }
  }
}
</script>

<style scoped>

</style>